<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="维修编号" prop="repairId">
        <el-input
          v-model="queryParams.repairId"
          placeholder="请输入维修编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维修日期" prop="repairTime">
        <el-date-picker clearable
          v-model="queryParams.repairTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择维修日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="维修方式" prop="repairWay">
        <el-select v-model="queryParams.repairWay" placeholder="请选择维修方式" clearable style="width: 200px;"
       
         :filterable="true"
        >
          <el-option
            v-for="dict in maintenance"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户" prop="userId">
      
         <el-select v-model="queryParams.userId" placeholder="请选择用户" clearable style="width: 200px;" filterable="true"
         >
          <el-option
            v-for="dict in userList"
            :key="dict.id"
            :label="dict.nickName"
            :value="dict.id"
             :filterable="true"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="取车位置" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入取车位置"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单日期" prop="reservationTime">
        <el-date-picker clearable
          v-model="queryParams.reservationTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择下单日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="车辆信息" prop="carInfoId">
       
         <el-select v-model="queryParams.carInfoId" placeholder="请选择车辆信息" clearable style="width: 200px;"
          :filterable="true" >
          <el-option
            v-for="dict in carInfoList"
            :key="dict.id"
            :label="dict.carNum"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="维修状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择维修状态" clearable style="width: 200px;"
         :filterable="true">
          <el-option
            v-for="dict in repair_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商家" prop="merchantid"
       :filterable="true">
        <el-select v-model="queryParams.merchantid" placeholder="请选择商家" clearable style="width: 200px;"
         :filterable="true">
          <el-option
            v-for="dict in merchantList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['evaluate:reservation:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['evaluate:reservation:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['evaluate:reservation:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['evaluate:reservation:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reservationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="维修编号" align="center" prop="repairId" />
      <el-table-column label="维修日期" align="center" prop="repairTime" >
        <template #default="scope">
          <span>{{ parseTime(scope.row.repairTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修方式" align="center" prop="repairWay">
        <template #default="scope">
          <dict-tag :options="maintenance" :value="scope.row.repairWay"/>
        </template>
      </el-table-column>
      <el-table-column label="用户" align="center" prop="customer.nickName" />
      <!-- <el-table-column label="取车位置" align="center" prop="location" /> -->
      <el-table-column label="下单日期" align="center" prop="reservationTime" >
        <template #default="scope">
          <span>{{ parseTime(scope.row.reservationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车辆信息" align="center" prop="carInfo.carNum" />
      <el-table-column label="整车照片" align="center" prop="vehiclePhotos" >
        <template #default="scope">
         
      <image-preview :src="scope.row.vehiclePhotos" :width="50" :height="50" />
        </template>

      </el-table-column>
      <el-table-column label="维修照片" align="center" prop="repairPhotos" >
        <!-- <template #default="scope">
          <img :src="scope.row.repairPhotos" style="width: 100px; height: 100px;"></template> -->
      <template #default="scope">
          <image-preview :src="scope.row.repairPhotos" :width="50" :height="50" />
        </template>
  
        
      </el-table-column>
      <el-table-column label="维修状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="repair_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="商家id" align="center" prop="merchant.name" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['evaluate:reservation:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['evaluate:reservation:remove']">删除</el-button>
          <el-button link type="primary" icon="Delete" @click="show(scope.row)" v-hasPermi="['evaluate:reservation:remove']">位置地址</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维修预约对话框 -->
    <el-dialog :title="title" v-model="open" width="700px"  append-to-body >

      <el-form ref="reservationRef" :model="form" :rules="rules" label-width="80px" class="buju">
        <el-form-item label="维修编号" prop="repairId">
          <el-input v-model="form.repairId" placeholder="请输入维修编号" style="width: 220px;" readonly />
        </el-form-item>
        <el-form-item label="车辆信息" prop="carInfoId" >
              <el-select v-model="form.status" placeholder="请选择维修状态" style="width: 220px;">
            <el-option
              v-for="dict in sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维修日期" prop="repairTime">
          <el-date-picker clearable
            v-model="form.repairTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择维修日期">
          </el-date-picker>
        </el-form-item>
         <el-form-item label="下单日期" prop="reservationTime">
          <el-date-picker clearable
            v-model="form.reservationTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择下单日期">
          </el-date-picker>
        </el-form-item>  
        <el-form-item label="维修方式" prop="repairWay">
      
              <el-select v-model="form.status" placeholder="请选择维修状态" style="width: 220px;">
            <el-option
              v-for="dict in sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
         <el-form-item label="维修状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择维修状态" style="width: 220px;">
            <el-option
              v-for="dict in sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户" prop="userId">
         
              <el-select v-model="form.status" placeholder="请选择维修状态" style="width: 220px;">
            <el-option
              v-for="dict in sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
         <el-form-item label="商家" prop="merchantid">
         
              <el-select v-model="form.status" placeholder="请选择维修状态" style="width: 220px;">
            <el-option
              v-for="dict in sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
         <el-form-item label="取车位置" prop="location" style="width: 600px;">
           <editor v-model="form.location" :min-height="192"/>
        </el-form-item>  
        <el-form-item label="整车照片" prop="vehiclePhotos" style="width: 300px;">
       
          <image-upload v-model="form.vehiclePhotos" />
        </el-form-item>
        <el-form-item label="维修照片" prop="repairPhotos" style="width:300px;">        
             <image-upload v-model="form.repairPhotos" />
        </el-form-item>  
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>


<!-- 展示详情 -->
 <el-dialog :title="title" v-model="open1" width="700px"  append-to-body>
   <el-form ref="reservationRef" :model="form" :rules="rules" label-width="80px">
         <el-form-item label="取车位置" prop="location" style="width: 600px;">
           <editor v-model="form.location" :min-height="192"/>
        </el-form-item>  
  </el-form>

        <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
         </template>
    
    </el-dialog>

  </div>
</template>

<script setup name="Reservation">
import { listReservation, getReservation, delReservation, addReservation, updateReservation } from "@/api/evaluate/reservation"
import { listMerchant } from  "@/api/merchant/merchant"
import {listInfo} from "@/api/bigbase/info"
import{listCarInfo} from "@/api/bigbase/carInfo"


const { proxy } = getCurrentInstance()
const { sys_user_sex } = proxy.useDict('sys_user_sex')
const { maintenance}=proxy.useDict('maintenance')
const {repair_status}=proxy.useDict('repair_status')
const reservationList = ref([])
const open = ref(false)
const open1 = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    repairId: null,
    repairTime: null,
    repairWay: null,
    userId: null,
    location: null,
    reservationTime: null,
    carInfoId: null,
    status: null,
    merchantid: null
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询维修预约列表 */
function getList() {
  loading.value = true
  listReservation(queryParams.value).then(response => {
    reservationList.value = response.rows
    total.value = response.total
    loading.value = false
    console.log(reservationList.value)
  })
}

// 取消按钮
function cancel() {
  open.value = false
  open1.value=false
  reset()
}



// 表单重置
function reset() {
  form.value = {
    id: null,
    repairId: null,
    repairTime: null,
    repairWay: null,
    userId: null,
    location: null,
    reservationTime: null,
    carInfoId: null,
    vehiclePhotos: null,
    repairPhotos: null,
    status: null,
    merchantid: null,
    customer:null
  }
  proxy.resetForm("reservationRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加维修预约"

}
// 查看详情
function show(row) {
  reset()
  const _id = row.id || ids.value
  getReservation(_id).then(response => {
    form.value = response.data
    open1.value = true
  title.value = "查看位置信息"
  })

}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getReservation(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改维修预约"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["reservationRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateReservation(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addReservation(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除维修预约编号为"' + _ids + '"的数据项？').then(function() {
    return delReservation(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('evaluate/reservation/export', {
    ...queryParams.value
  }, `reservation_${new Date().getTime()}.xlsx`)
}

const  q=ref({
  pageNum: 1,
  pageSize: 100000,
 
})
const merchantList = ref([])
const userList = ref([])
const carInfoList = ref([])
// 商家
function getMerchantList() {
  listMerchant(q.value).then(response => {
  
    
    merchantList.value = response.rows
    console.log("sssssssssssss",merchantList.value);
    
  })
}

//用户
function getUserList() {
 listInfo(q.value).then(response => {
    userList.value = response.rows
    console.log("ddddddddddd",userList.value);
    
  })
}
//车辆信息
function getCarInfoList() {
  listCarInfo().then(response => {
    carInfoList.value = response.rows
    console.log("cccccccccccc",carInfoList.value);
    
  })
}


getCarInfoList()
getMerchantList()
getUserList()
getList()










</script>
<style >
.buju{
  display: flex;
  flex-wrap: wrap;
}
</style>
